<template>
  <div class="header">
    <svg class="goods-svg" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <defs>
        <symbol id="icon-arrow-short" viewBox="0 0 25 32">
          <title>arrow-short</title>
          <path
            d="M24.487 18.922l-1.948-1.948-8.904 8.904v-25.878h-2.783v25.878l-8.904-8.904-1.948 1.948 12.243 12.243z"></path>
        </symbol>
        <symbol id="icon-status-ok" viewBox="0 0 32 32">
          <title>status-ok</title>
          <path
            d="M22.361 10.903l-9.71 9.063-2.998-2.998c-0.208-0.209-0.546-0.209-0.754 0s-0.208 0.546 0 0.754l3.363 3.363c0.104 0.104 0.241 0.156 0.377 0.156 0.131 0 0.261-0.048 0.364-0.143l10.087-9.414c0.215-0.201 0.227-0.539 0.026-0.754s-0.539-0.226-0.754-0.026z"></path>
          <path
            d="M16 30.933c-8.234 0-14.933-6.699-14.933-14.933s6.699-14.933 14.933-14.933c8.234 0 14.933 6.699 14.933 14.933s-6.699 14.933-14.933 14.933zM16 0c-8.822 0-16 7.178-16 16 0 8.823 7.178 16 16 16s16-7.177 16-16c0-8.822-7.178-16-16-16z"></path>
        </symbol>
        <symbol id="icon-cart" viewBox="0 0 38 32">
          <title>cart</title>
          <path
            d="M37.759 0h-4.133c-0.733 0.004-1.337 0.549-1.434 1.255l-0.546 4.342c-0.081 0.484-0.496 0.849-0.997 0.849-0.005 0-0.009-0-0.014-0h-27.604c-0.003 0-0.007-0-0.011-0-1.674 0-3.031 1.357-3.031 3.031 0 0.34 0.056 0.666 0.159 0.971l2.52 8.062c0.385 1.194 1.486 2.043 2.785 2.043 0.126 0 0.25-0.008 0.372-0.023l22.983 0.002c0.515 0.131 0.626 0.768 0.626 1.283 0.005 0.044 0.009 0.095 0.009 0.146 0 0.501-0.294 0.933-0.718 1.134l-22.439 0.003c-0.354 0-0.642 0.287-0.642 0.642s0.287 0.642 0.642 0.642h22.745l0.131-0.071c0.919-0.392 1.551-1.287 1.551-2.33 0-0.058-0.002-0.116-0.006-0.173 0.021-0.108 0.033-0.24 0.033-0.376 0-1.072-0.732-1.973-1.724-2.23l-23.357-0.004c-0.063 0.008-0.135 0.013-0.209 0.013-0.719 0-1.332-0.455-1.566-1.093l-2.53-8.095c-0.048-0.154-0.076-0.332-0.076-0.515 0-0.973 0.782-1.764 1.752-1.778h27.657c1.159-0.004 2.112-0.883 2.232-2.011l0.547-4.345c0.010-0.083 0.078-0.147 0.161-0.152l4.133-0c0.354 0 0.642-0.287 0.642-0.642s-0.287-0.642-0.642-0.642z"></path>
          <path
            d="M31.323 9.69c-0.022-0.003-0.048-0.004-0.074-0.004-0.328 0-0.598 0.248-0.633 0.567l-0.809 7.268c-0.003 0.022-0.004 0.048-0.004 0.074 0 0.328 0.248 0.598 0.567 0.633l0.074 0c0.001 0 0.003 0 0.004 0 0.327 0 0.596-0.246 0.632-0.563l0.809-7.268c0.003-0.022 0.004-0.048 0.004-0.074 0-0.328-0.248-0.598-0.567-0.633z"></path>
          <path
            d="M27.514 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM27.514 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
          <path
            d="M9.599 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM9.599 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
        </symbol>
      </defs>
    </svg>
    <div class="navbar">
      <div class="navbar-left-container">
        <a href="/">
          <img class="navbar-brand-logo" src="./mi-logo.png">
        </a>
      </div>
      <div class="navbar-right-container">
        <div class="navbar-menu-container">
          <!--<a href="/" class="navbar-link">我的账户</a>-->
          <span class="navbar-link" v-text="nickName.userName" v-if="nickName"></span>
          <a href="javascript:void(0)" class="navbar-link" @click="mdShow=true" v-if="!nickName">登录</a>
          <a href="javascript:void(0)" class="navbar-link" @click="logOut" v-else>登出</a>
          <div class="navbar-cart-container">
            <span class="navbar-cart-count" v-text="cartCount" v-if="cartCount"></span>
            <a class="navbar-link" href="/#/cart">
              <svg class="navbar-cart-logo">
                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-cart"></use>
              </svg>
            </a>
          </div>
        </div>
      </div>
    </div>
    <!--模态框-->
    <v-modal :mdShow="mdShow" @close="closeModal">
      <div class="md-title" slot="mdTitle">登录</div>
      <div slot="message">
        <div class="error-wrap">
          <span class="error error-show" v-show="errorTip">用户名或者密码错误</span>
        </div>
        <ul>
          <li class="regi_form_input">
            <i class="icon IconPeople"></i>
            <input type="text"
                   name="loginname"
                   class="regi_login_input regi_login_input_left"
                   placeholder="用户名"
                   v-model="userName"
                   autocomplete="new-password"
                   data-type="loginname">
          </li>
          <li class="regi_form_input noMargin">
            <i class="icon IconPwd"></i>
            <input type="password"
                   name="password"
                   v-model="userPwd"
                   autocomplete="new-password"
                   class="regi_login_input regi_login_input_left login-input-no input_text"
                   placeholder="密码">
          </li>
        </ul>
      </div>
      <div slot="btnGroup">
        <div class="login-wrap">
          <a href="javascript:;" class="btn-login" @click="login">登  录</a>
        </div>
      </div>
    </v-modal>
  </div>
</template>

<script type="text/ecmascript-6">
  import modal from '../modal/modal'
  import axios from 'axios'
  import {mapState} from 'vuex'

  export default{
    data () {
      return {
        mdShow: false,
        errorTip: false,
        userName: '',
        userPwd: ''
      }
    },
    computed: {
      ...mapState(['nickName', 'cartCount'])
    },
    mounted () {
      this.checkLogin();
    },
    methods: {
      checkLogin () {
        axios.get('/users/checkLogin').then((response) => {
          var res = response.data;
          // var path = this.$route.pathname;
          if (res.status === '0') {
            // this.nickName = res.result;
            this.$store.commit('updateUserInfo', res.result);
            this.mdShow = false;
          } else {
            if (this.$route.path !== '/') {
              this.$router.push('/');
            }
          }
        });
      },
      login () {
        if (!this.userName || !this.userPwd) {
          this.errorTip = true;
          return;
        }
        axios.post('/users/login', {
          userName: this.userName,
          userPwd: this.userPwd
        }).then((response) => {
          var res = response.data;
          if (res.status === '0') {
            this.errorTip = false;
            this.mdShow = false;
            this.$store.commit('updateUserInfo', res.result);
            this.getCartCount();
          } else {
            this.errorTip = true;
          }
        });
      },
      logOut () {
        axios.post('/users/logout').then((response) => {
          let res = response.data;
          if (res.status === '0') {
            // this.nickName = '';
            this.$store.commit('updateUserInfo', res.result.userName);
            this.$store.commit('resetCartCount', 0);
            if (this.$route.path !== '/') {
              this.$router.push('/');
            }
          }
        })
      },
      closeModal () {
        this.mdShow = false;
      },
      getCartCount () {
        axios.get('/users/getCartCount').then(res => {
          var result = res.data;
          this.$store.commit('updateCartCount', result.result);
        });
      }
    },
    components: {
      'v-modal': modal
    }
  }
</script>

<style rel="stylesheet">
  .header {
    width: 100%;
    background-color: white;
    font-family: "moderat", sans-serif;
    font-size: 16px;
  }

  .goods-svg {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
  }

  .navbar {
    display: flex;
    justify-content: space-between;
    align-content: center;
    width: 100%;
    height: 70px;
    max-width: 1280px;
    margin: 0 auto;
    padding: 5px 20px 10px 20px;
  }

  .navbar-left-container {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-left: -20px;
  }

  .header a, .footer a {
    color: #666;
    text-decoration: none;
  }

  .navbar-brand-logo {
    /*width: 120px;*/
    margin-top: 10px;
    background: #FF6700;
  }

  a {
    -webkit-transition: color .3s ease-out;
    transition: color .3s ease-out;
  }

  .navbar-right-container {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }

  .navbar-menu-container {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-top: 10px;
  }

  .navbar-link {
    padding-left: 15px;
  }

  .navbar-cart-container {
    position: relative;
  }

  .navbar-cart-count {
    justify-content: center;
    align-items: center;
    position: absolute;
    top: -9px;
    right: -11px;
    width: 20px;
    border-radius: 10px;
    color: white;
    background-color: #eb767d;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
  }

  .navbar-cart-logo {
    width: 25px;
    height: 25px;
    transform: scaleX(-1);
  }

  .regi_form_input {
    position: relative;
    height: 42px;
    line-height: 42px;
    background: none;
    margin-bottom: 15px;
    font-size: 14px;
    overflow: hidden;
    border: 1px solid #ccc;
    padding-bottom: 0;
  }

  .regi_form_input .icon {
    display: inline-block;
    float: left;
    width: 25px;
    height: 29px;
    margin: 6px 0 0 14px;
    background: url(/static/img/icon.png) no-repeat 4px 5px;
  }

  .regi_form_input .IconPwd {
    background-position: -198px 3px;
  }

  .regi_form_input .regi_login_input {
    position: absolute;
    left: 45px;
    top: 0;
    padding: 9px 0 10px;
    width: 270px;
    font-size: 14px;
    zoom: 1;
    border: none;
    color: #333;
    line-height: 23px;
    background: 0 0 !important;
    background: none;
  }

  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px white inset;
  }

  .md-title {
    position: absolute;
    top: 14px;
    line-height: 24px;
    padding: 8px 0;
    color: #333;
    font-size: 18px;
    font-weight: 400;
    font-style: normal;
  }

  .md-content .btn-login {
    display: block;
    height: 38px;
    line-height: 38px;
    border: 2px solid #009de6;
    background: #009de6;
    color: #fff;
    font-size: 18px;
    text-align: center;
  }

  .login-wrap {
    margin-top: 30px;
  }
</style>
